//
//  CAShopCartTableViewCell.m
//  LNH
//
//  Created by twentylx on 2018/1/18.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAShopCartTableViewCell.h"

@implementation CAShopCartTableViewCell

- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview:self.selectButton];
        [self addSubview:self.goodsImageView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.sizeLabel];
        [self addSubview:self.priceLabel];
        [self addSubview:self.stepper];
        [self addSubview:self.delButton];
        [self addSubview:self.lineView];
        
        [_selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        
        [_goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.selectButton.mas_right).offset(10);
             make.top.equalTo(self).offset(25);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(100);
        }];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.goodsImageView.mas_right).offset(5);
            make.top.equalTo(self.goodsImageView);
            make.right.equalTo(self).offset(-50);
        }];
        
        [_sizeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
            make.right.equalTo(self).offset(-100);
        }];
        
        [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLabel);
            make.top.equalTo(self.sizeLabel.mas_bottom).offset(10);
        }];
        
        [_stepper mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-30);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(80, 20));
        }];
        
        [_delButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-20);
            make.top.equalTo(self.stepper.mas_bottom).offset(20);
            make.width.mas_equalTo(60);
        }];
        
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(ONE_PIXEL);
        }];
        
    }
    return self;
}

#pragma mark get & set
- (UIButton *) selectButton {
    if (!_selectButton) {
        _selectButton = [UIButton new];
        [_selectButton setImage:LMImageNamed(@"shopcart_selectNormal") forState:UIControlStateNormal];
        [_selectButton setImage:LMImageNamed(@"shopcart_select") forState:UIControlStateSelected];
//        [_selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _selectButton;
}

- (UIImageView *) goodsImageView {
    if (!_goodsImageView) {
        _goodsImageView = [UIImageView new];
        _goodsImageView.contentMode = UIViewContentModeScaleAspectFit;
        _goodsImageView.layer.borderWidth = 1;
    }
    return _goodsImageView;
}

- (UILabel *) titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
//        _titleLabel.text = @"swarovski施华洛世奇 新款15512-9钻戒";
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.numberOfLines = 2;
        _titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return _titleLabel;
}

- (UILabel *) sizeLabel {
    if (!_sizeLabel) {
        _sizeLabel = [UILabel new];
//        _sizeLabel.text = @"重量：0.36g";
        _sizeLabel.textColor = RGBCOLOR(158, 158, 159);
        _sizeLabel.font = [UIFont systemFontOfSize:12];
    }
    return _sizeLabel;
}

- (UILabel *) priceLabel {
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
//        _priceLabel.text = @"￥399.00";
        _priceLabel.textColor = RGBCOLOR(233, 85, 19);
        _priceLabel.font = [UIFont systemFontOfSize:14];
    }
    return _priceLabel;
}

-(PKYStepper *) stepper {
    if (!_stepper) {
        _stepper = [PKYStepper new];
        _stepper.minimum = 1;
        _stepper.maximum = 99999;
        _stepper.countLabel.textColor = [UIColor blackColor];
        _stepper.countLabel.font = FS(14);
        [_stepper.countLabel setBackgroundColor:RGBCOLOR(245, 245, 245)];
        [_stepper.countLabel.layer setBorderWidth:ONE_PIXEL];
        [_stepper.countLabel.layer setBorderColor:RGBCOLOR(229, 229, 229).CGColor];
        [_stepper setBorderWidth:ONE_PIXEL];
        [_stepper setBorderColor: RGBCOLOR(229, 229, 229)];
        
        [_stepper.decrementButton setTitleColor:RGBACOLOR(238, 128, 16,1) forState:UIControlStateNormal];
        [_stepper.incrementButton setTitleColor:RGBACOLOR(238, 128, 16,1) forState:UIControlStateNormal];
        [_stepper.decrementButton setBackgroundColor:RGBCOLOR(250, 250, 250)];
        [_stepper.incrementButton setBackgroundColor:RGBCOLOR(250, 250, 250)];
        
        [_stepper setButtonFont: FS(20)];
        _stepper.buttonWidth = 30*SCALE;
//        [_stepper setValue:1];
    }
    return _stepper;
}

- (UIButton *) delButton {
    if (!_delButton) {
        _delButton = [UIButton new];
        [_delButton setImage:LMImageNamed(@"shopcart_del") forState:UIControlStateNormal];
        [_delButton setTitle:@"删除" forState:UIControlStateNormal];
        _delButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_delButton setTitleColor:RGBCOLOR(35, 24, 21) forState:UIControlStateNormal];
        _delButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0,0);
        _delButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _delButton;
}

- (UIView *) lineView {
    if (!_lineView) {
        _lineView = [UIView new];
        [_lineView setBackgroundColor:RGBCOLOR(51, 51, 51)];
    }
    return _lineView;
}

#pragma mark configForCell
- (void)configForCell:(NSDictionary *)model {
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", BASE_IMAGE_DOMAIN, model[@"goods"][@"goods_img"]]]];
    self.titleLabel.text = [NSString stringWithFormat:@"%@", model[@"goods"][@"name"]];
    NSArray *attrArray = model[@"goods_attr"];
    __block NSString *attrStr = @"";
    [attrArray enumerateObjectsUsingBlock:^(NSDictionary *attrDict, NSUInteger idx, BOOL * _Nonnull stop) {
        attrStr = [NSString stringWithFormat:@"%@ %@:%@", attrStr, attrDict[@"name"], attrDict[@"attr_value"]];
    }];
    self.sizeLabel.text = attrStr;
    self.priceLabel.text = [NSString stringWithFormat:@"%@", model[@"goods"][@"goods_price"]];
    self.stepper.countLabel.text = [NSString stringWithFormat:@"%ld", [model[@"number"] integerValue]];
    [self.stepper setValue:[model[@"number"] floatValue]];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
